package com.orderplus.opshop.synces.controller;

import com.orderplus.opshop.core.data.request.RequestParam;
import com.orderplus.opshop.core.data.response.ResponseData;
import com.orderplus.opshop.core.page.QueryParameter;
import com.orderplus.opshop.synces.constant.EsConstant;
import com.orderplus.opshop.synces.entity.product.Product;
import com.orderplus.opshop.synces.service.elasticsearch.data.QueryParamData;
import com.orderplus.opshop.synces.service.elasticsearch.data.QueryResultData;
import com.orderplus.opshop.synces.service.elasticsearch.impl.OpSearchService;
import com.orderplus.opshop.synces.service.product.ProductService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping(value = "opSearch", produces = "Application/json;charset=UTF-8")
public class OpSearchController {

	private final ProductService productService;

	private final OpSearchService opSearchService;

	@GetMapping("/page")
	public ResponseData page(QueryParameter page, Product product) {
		RequestParam<QueryParamData> request = null;
		try {
			request = opSearchService.setDefaultParam(EsConstant.ES_INDICES, new String[]{}, new String[]{}, page.getPageNo(), page.getPageSize());
		} catch (IOException e) {
			e.printStackTrace();
		}

		ResponseData<QueryResultData.ListQueryResultData> response = opSearchService.call(request);

		return response;
	}

	@PostMapping("/syncProduct")
	public void syncProduct(String fromTabe, String targetTable, String primaryKey, int size) {
		productService.syncToEsByProduct(fromTabe, targetTable, primaryKey, size);
	}

}